//
//  ResultViewController.m
//  PureProject
//
//  Created by Tung Tran on 4/22/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ResultViewController.h"
#import "HeaderAnalysisViewController.h"
#import "ResultCellViewController.h"
#import "JSON.h"



@interface ResultViewController ()

@end

@implementation ResultViewController

@synthesize jsonData = _jsonData;
@synthesize responseData = _responseData;
@synthesize resultTableView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    // Set title
    self.title = @"Result";
    
    // Item search back
    UIBarButtonItem *itemBack = [[UIBarButtonItem alloc] initWithTitle:@"Search" style:UIBarButtonItemStyleBordered target:self action:@selector(itemBackIsPressed:)];
    self.navigationItem.leftBarButtonItem = itemBack;
    [itemBack release];
    NSURLRequest *request = [NSURLRequest requestWithURL:  
                             [NSURL URLWithString:URL_GET_UNITS]];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
	return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    //NSLog(@"COUNT %i", [self.jsonData count]);
    return [self.jsonData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ResultCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        //cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        UIViewController *viewController = [[UIViewController alloc] initWithNibName:@"ResultCellViewController" bundle:nil];
        cell = (UITableViewCell *)viewController.view;
    }  
     UILabel *lb1 = (UILabel *)[cell viewWithTag:10];
     UILabel *lb2 = (UILabel *)[cell viewWithTag:11];
     UILabel *lb3 = (UILabel *)[cell viewWithTag:12];
     UILabel *lb4 = (UILabel *)[cell viewWithTag:13];
     UILabel *lb5 = (UILabel *)[cell viewWithTag:14];
     UILabel *lb6 = (UILabel *)[cell viewWithTag:15];
     UILabel *lb7 = (UILabel *)[cell viewWithTag:16];
     UILabel *lb8 = (UILabel *)[cell viewWithTag:17];
     UILabel *lb9 = (UILabel *)[cell viewWithTag:18];
     UILabel *lb10 = (UILabel *)[cell viewWithTag:19];
     UILabel *lb11 = (UILabel *)[cell viewWithTag:20];
     UILabel *lb12 = (UILabel *)[cell viewWithTag:21];
     UILabel *lb13 = (UILabel *)[cell viewWithTag:22];
     UILabel *lb14 = (UILabel *)[cell viewWithTag:23];
    
    //[cell.textLabel setText:@"aaaa"];
    NSDictionary *dataIndex = [self.jsonData objectAtIndex:[indexPath row]];
    
    [lb1 setText:[dataIndex objectForKey:@"unit"]];
    [lb2 setText:[dataIndex objectForKey:@"name"]];
    [lb3 setText:[dataIndex objectForKey:@"level"]];
    [lb4 setText:[dataIndex objectForKey:@"sqm"]];
    [lb5 setText:[dataIndex objectForKey:@"unit_type"]];
    [lb6 setText:[dataIndex objectForKey:@"lease_status"]];
    [lb7 setText:[dataIndex objectForKey:@"status"]];
    [lb8 setText:[dataIndex objectForKey:@"reason"]];
    [lb9 setText:[dataIndex objectForKey:@"w"]];
    [lb10 setText:[dataIndex objectForKey:@"l"]];
    [lb11 setText:[dataIndex objectForKey:@"b"]];
    [lb12 setText:[dataIndex objectForKey:@"no_type"]];
    [lb13 setText:[dataIndex objectForKey:@"next_step"]];
    [lb14 setText:[dataIndex objectForKey:@"tc"]];

    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
	return 22;
}

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    HeaderAnalysisViewController *headerView = [[[HeaderAnalysisViewController alloc] initWithNibName:@"HeaderAnalysisViewController" bundle:nil] autorelease];
    
    
    return headerView.view;
}

#pragma mark - Navigation bar Item
- (void)itemBackIsPressed:(id)sender
{
    [self dismissModalViewControllerAnimated:YES];
}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
	NSLog(@"connectionDidFinishLoading");
    
    NSString *strData = [[NSString alloc] initWithData:self.responseData encoding:NSASCIIStringEncoding];
    
    self.jsonData = [strData JSONValue];
    //NSLog(@"data = %@", self.jsonData.description);
    //NSLog(@"%@",self.jsonData.description);
    
    /*
     self.books = [[NSMutableArray alloc] initWithObjects: nil];
     for (NSInteger idB = 0; idB < [jsonData count]; idB++) {
     NSDictionary *book = [jsonData objectAtIndex:idB];
     
     NSString *idBook = [book objectForKey:@"id"];
     NSString *nameBook = [book objectForKey:@"title"];
     NSString *imgPath = [[book objectForKey:@"image"] stringByReplacingOccurrencesOfString:@"\\/" withString:@"\\"];
     NSString *filePath = [[book objectForKey:@"file"] stringByReplacingOccurrencesOfString:@"\\/" withString:@"\\"];
     
     Book *newBook = [[Book alloc] initWithIdBook:[idBook intValue] NameBook:nameBook ImageCoverPath:imgPath FilePath:filePath];
     [self.books addObject:newBook];
     [newBook release];
     }
     
     [self.tableView reloadData];
     */
    [self.resultTableView reloadData];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
	NSLog(@"connectionDidReceiveResponse");
	self.responseData = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
	NSLog(@"connectionDidReceiveData");
	[self.responseData appendData:data];
	
	NSString *str = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
	NSLog(@"data = %@", str);
	//resultField.text = str;
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
	NSLog(@"fail with error!!");
}



@end
